package com.flexcloudsdk.memcache;
import com.flexcloudsdk.web.CloudAPIIdentityServlet;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.google.appengine.api.memcache.MemcacheService.SetPolicy;
import com.google.appengine.api.memcache.ErrorHandler;
import com.google.appengine.api.memcache.MemcacheService.IdentifiableValue;
import com.google.appengine.api.memcache.Stats;
import com.google.appengine.api.memcache.Expiration;


import java.io.Serializable;
import java.util.Collection;
import java.util.Set;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

@SuppressWarnings("unused")
public class CloudMemcache {
	private static final Logger log = Logger.getLogger(CloudMemcache.class.getName());
	private MemcacheService service;
	private String sourceMethod;
	private String sourceClass;

	
	public CloudMemcache() {
		log.info("CloudMemcache has been created by BlazeDS");
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		sourceClass = CloudMemcache.class.getName();
		service = MemcacheServiceFactory.getMemcacheService();
	}
	
	public void clearAll() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		service.clearAll();
	}
	
	public boolean contains(Object key) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		return service.contains(key);
	}
	
	public boolean delete(Object key) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		return service.delete(key);
	}
	
	public boolean delete(Object key, long millisNoReAdd) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		return service.delete(key);
	}
	
	
	public Set<?> deleteAll(Collection<Object> keys) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		return service.deleteAll(keys);
	}
	
	public Set<?> deleteAll(Collection<Object> keys, long millisNoReAdd) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		return service.deleteAll(keys, millisNoReAdd);
	}
	
	public Object get(Object key) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		return service.get(key);
	}
	
	public Map<?, Object> getAll(Collection<?> key) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		return service.getAll(key);
	}
	
	public ErrorHandler getErrorHandler() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);
		return service.getErrorHandler();
	}
	
	public  IdentifiableValue getIdentifiable(Object key) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);
		return service.getIdentifiable(key);
	}
	
	public String getNamespace() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);
		return service.getNamespace();
	}
	
	public Stats getStatistics() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);
	
		return service.getStatistics();
	}
	
	public  long increment(Object key, long delta) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		return service.increment(key, delta);
	}
	
	public Long increment(Object key, long delta, Long initialValue) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		return service.increment(key, delta, initialValue);
	}
	
	public Map<?, Long> incrementAll(Map<?, Long> offsets) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		return service.incrementAll(offsets);
	}
	
	public Map<?, Long>  incrementAll(Collection<?> Keys, long delta) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		return service.incrementAll(Keys, delta);
	}
	
	public Map<?, Long>  incrementAll(Map<?, Long> offsets, Long initialValue) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		return service.incrementAll(offsets, initialValue);
	}
	
	
	public void get(Object key, Object value, Expiration expires, SetPolicy policy) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		service.put(key, value, expires, policy);
	}

	public void put(Object key, Object value) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		service.put(key, value);
	}
	
	public void put (Object key, Object value, Expiration expires) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		service.put(key, value, expires);
	}
	
	public boolean put(Object key, Object value, Expiration expires, SetPolicy policy) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		return service.put(key, value, expires, policy);
	}
	
	public void putAll(Map<?, ?> values) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);
		
		service.putAll(values);
	}
	
	public void putAll(Map<?, ?> values, Expiration expires) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		service.putAll(values, expires);
	}
	
	public Set<?> putAll(Map<Object, ?> values, Expiration expires, SetPolicy policy) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		return service.putAll(values, expires, policy);
	}
	
	public boolean putIfUntouched(Object key, IdentifiableValue oldValue, Object newValue) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		return service.putIfUntouched(key, oldValue, newValue);
	}
	
	public boolean putIfUntounched(Object key, IdentifiableValue oldValue, Object newValue, Expiration expires) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		return service.putIfUntouched(key, oldValue, newValue, expires);
	}
	
	public void setErrorHandler(ErrorHandler handler) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info(sourceClass + ":" +  sourceMethod);

		service.setErrorHandler(handler);
	}
	
}
